package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterAssetQueryExecutor;
import licode.unisop.authcenter.application.info.*;
import licode.unisop.client.info.SopTokenInfo;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.client.utils.SopRequestParse;
import licode.unisop.provider.vo.SiAsset;
import licode.unisop.provider.vo.SiAssetBrief;
import licode.unisop.provider.vo.SiPageResult;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/asset")
public class CenterAssetQueryController {
    @Resource
    private HttpServletRequest request;

    @Resource
    private CenterAssetQueryExecutor centerAssetQueryExecutor;

    /**
     * 分页查询平台资源信息
     */
    @RequestMapping(value = "/query/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public SiPageResult<SiAssetBrief> queryPage(@RequestBody PlatAssetQueryPageIn req) {
        SopTokenInfo tokenInfo = SopRequestParse.getAccessTokenInfo(request);
        return centerAssetQueryExecutor.loadAssetByPage(tokenInfo, req);
    }

    /**
     * 分页查询平台可移动资源信息
     */
    @RequestMapping(value = "/query/move/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public SiPageResult<SiAssetBrief> queryMovePage(@RequestBody PlatMoveAssetQueryPageIn req) {
        return centerAssetQueryExecutor.queryMovePage(req);
    }

    /**
     * 按层级获取指定类型的资源
     */
    @RequestMapping(value = "/query/level/all", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public List<SiAsset> queryAllByLevel(@RequestBody PlatAllAssetByLevelIn req) {
        return centerAssetQueryExecutor.queryAllByLevel(req);
    }

    /**
     * 按层级分页查询资源信息，必须传入父资源ID，如果父资源ID为空，则查询顶层所有资源
     */
    @RequestMapping(value = "/query/level/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public SiPageResult<SiAssetBrief> queryPageByLevel(@RequestBody PlatAssetByLevelPageIn req) {
        return centerAssetQueryExecutor.queryPageByLevel(req);
    }

    /**
     * 分页查询指定资源拥有的所有资源信息，比如查询指定分组下所有的权限信息
     */
    @RequestMapping(value = "/relation/of-holder/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public SiPageResult<SiAssetBrief> queryAssetsOfHolderPage(@RequestBody AssetsOfHolderPageIn req) {
        return centerAssetQueryExecutor.queryAssetsOfHolderPage(req);
    }

    /**
     * 分页查询拥有指定资源的所有资源信息，比如查询拥有指定权限的所有分组信息
     */
    @RequestMapping(value = "/relation/to-holders/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public SiPageResult<SiAssetBrief> queryHoldersOfAssetPage(@RequestBody HoldersOfAssetPageIn req) {
        return centerAssetQueryExecutor.queryHoldersOfAssetPage(req);
    }

    /**
     * 读取资源属性
     */
    @RequestMapping(value = "/query/attribute/load", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "asset:query")
    public Map<String, Object> loadAttributesOfAsset(@RequestBody AttributesOfAssetQueryIn req) {
        Map<String, Object> attrs = centerAssetQueryExecutor.loadAttributesOfAsset(req);
        if (null != attrs) {
            attrs.remove("createTime");
            attrs.remove("updateTime");
            attrs.remove("lastLoginTime");
        }
        return attrs;
    }
}
